<template>
  <div class="message" :class="{ 'user-message': message.isUser }">
    <div class="message-content">
      <div class="avatar" v-if="!message.isUser">
        <img src="@/assets/dear.png" alt="AI Avatar" class="avatar-img" />
      </div>
      <div class="bubble-wrapper">
        <div class="bubble">
          <MarkdownRenderer v-if="!message.isUser" :content="message.text" />
          <span v-else>{{ message.text }}</span>
        </div>
        <div class="message-time" :class="{ 'user-time': message.isUser }">
          {{ formatTime(message.timestamp) }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import MarkdownRenderer from './MarkdownRenderer.vue'

defineProps({
  message: {
    type: Object as () => {
      text: string
      isUser: boolean
      timestamp: Date
    },
    required: true
  }
})

const formatTime = (timestamp: Date) => {
  return new Date(timestamp).toLocaleTimeString()
}
</script>

<style scoped>
.message {
  margin: 8px 0;
  max-width: 85%;
  animation: fadeIn 0.3s ease;
}

.message-content {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.user-message {
  margin-left: auto;
  max-width: 60%;
}

.user-message .message-content {
  justify-content: flex-end;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.bubble-wrapper {
  flex: 1;
  min-width: 0;
}

.user-message .bubble-wrapper {
  flex: unset;
}

.bubble {
  padding: 14px 18px;
  position: relative;
  transition: all 0.3s ease;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
  font-size: 16px;
  line-height: 1.7;
  letter-spacing: 0.3px;
  word-spacing: 1px;
  color: #2c3e50;
}

.user-message .bubble {
  color: #6366f1;
  font-weight: 400;
  letter-spacing: 0.4px;
}

.message-time {
  font-size: 0.75rem;
  color: #666;
  margin-top: 4px;
  text-align: left;
}

.user-time {
  text-align: right;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

:deep(.v-theme--dark) {
  .avatar {
    background: #2d3748;
  }

  .bubble {
    color: #e2e8f0;
  }

  .user-message .bubble {
    color: #7c3aed;
  }

  .message-time {
    color: #a0aec0;
  }
}

:deep(.markdown-body) {
  background: transparent !important;
  color: inherit;
}

:deep(.markdown-body ol) {
  padding-left: 1.2em;
  margin: 0;
  list-style-position: outside;
}

:deep(.markdown-body ol li) {
  margin: 4px 0;
  padding-left: 0.3em;
}

:deep(.markdown-body pre) {
  background-color: rgba(0, 0, 0, 0.05);
}

.user-message :deep(.markdown-body) {
  color: white;
}

.user-message :deep(.markdown-body pre) {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-message :deep(.markdown-body code) {
  background-color: rgba(255, 255, 255, 0.2);
}

.user-message :deep(.markdown-body blockquote) {
  border-left-color: rgba(255, 255, 255, 0.3);
  color: rgba(255, 255, 255, 0.8);
}
</style> 